<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      label-width="auto"
      v-show="showSearch"
    >
      <el-row>
        <el-form-item label="查询日期" style="width: 328px">
          <el-date-picker
            v-model="dateRange"
            value-format="YYYY-MM-DD"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="纸张编码" prop="code">
          <el-input
            v-model.trim="queryParams.code"
            style="width: 180px"
            placeholder="请输入纸张编码"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="纸张名称" prop="name">
          <el-input
            v-model.trim="queryParams.name"
            style="width: 180px"
            placeholder="请输入纸张名称"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <div class="operation">
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          <el-button @click="advanced = !advanced">
            <a style="color: #448ef7">
              <el-icon v-if="advanced">
                <ArrowUpBold />
              </el-icon>
              <el-icon v-else>
                <ArrowDownBold />
              </el-icon>
            </a>
          </el-button>
          <right-toolbar
            style="margin-left: 10px"
            :search="false"
            v-model:showSearch="showSearch"
            @queryTable="getList"
            :columns="columns"
          ></right-toolbar>
        </div>
      </el-row>
      <el-row v-if="advanced">
        <el-form-item label="品牌" prop="brand">
          <el-select
            v-model="queryParams.brand"
            filterable
            clearable
            placeholder="选择品牌"
            style="width: 120px"
          >
            <el-option
              v-for="item in mes_paper_brand"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="纸张类型" prop="packageType">
          <el-select
            v-model="queryParams.packageType"
            filterable
            clearable
            placeholder="选择纸张类型"
            style="width: 120px"
          >
            <el-option
              v-for="item in mes_paper_package"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="规格型号" prop="specs">
          <el-select
            v-model="queryParams.specs"
            filterable
            clearable
            placeholder="选择规格型号"
            style="width: 150px"
          >
            <el-option
              v-for="item in mes_paper_specs"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="定量" prop="weight">
          <el-input
            type="number"
            v-model.trim="queryParams.weight"
            style="width: 120px"
            placeholder="请输入定量"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-select
            v-model="queryParams.unit"
            clearable
            placeholder="选择单位"
            style="width: 120px"
          >
            <el-option
              v-for="item in mes_paper_unit"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="幅宽" prop="longWide">
          <el-input
            v-model.trim="queryParams.longWide"
            clearable
            style="width: 120px"
            placeholder="请输入幅宽"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
      </el-row>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Download" @click="handleExport"
          >导出</el-button
        >
      </el-col>
    </el-row>
    <div>
      <el-table height="65vh" v-loading="loading" :data="dataList" border>
        <el-table-column label="序号" width="60" type="index" align="center" />
        <el-table-column
          v-if="columns[1].visible"
          label="平张/卷筒"
          min-width="95"
          align="center"
          prop="packageType"
          :show-overflow-tooltip="true"
        >
          <template #default="scope">
            <dict-tag
              :options="mes_paper_package"
              :value="scope.row.packageType"
            />
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[2].visible"
          label="纸张编码"
          min-width="200"
          align="center"
          prop="code"
          :show-overflow-tooltip="true"
        >
          <template v-slot="{ row }">
            <el-button link type="primary" @click="handleView(row)">{{
              row.code
            }}</el-button>
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[3].visible"
          label="纸张名称"
          min-width="280"
          align="center"
          prop="name"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          v-if="columns[3].visible"
          label="幅宽"
          min-width="60"
          align="center"
          prop="longWide"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          v-if="columns[4].visible"
          label="规格型号"
          clearable
          min-width="110"
          align="center"
          prop="specs"
          :show-overflow-tooltip="true"
        >
          <template #default="scope">
            <dict-tag :options="mes_paper_specs" :value="scope.row.specs" />
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[5].visible"
          label="定量"
          min-width="60"
          align="center"
          prop="weight"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          v-if="columns[6].visible"
          width="80"
          label="品牌"
          clearable
          align="center"
          prop="brand"
          :show-overflow-tooltip="true"
        >
          <template #default="scope">
            <dict-tag :options="mes_paper_brand" :value="scope.row.brand" />
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[7].visible"
          label="单位"
          width="60"
          align="center"
          prop="unit"
          :show-overflow-tooltip="true"
        >
          <template #default="scope">
            <dict-tag :options="mes_paper_unit" :value="scope.row.unit" />
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[8].visible"
          label="起初数量"
          min-width="100"
          align="center"
          prop="originalStock"
          :show-overflow-tooltip="true"
        />
        <el-table-column
          v-if="columns[9].visible"
          label="入库数量"
          min-width="100"
          align="center"
          prop="inNum"
          :show-overflow-tooltip="true"
        >
          <template v-slot="{ row }">
            <el-button link type="primary" @click="handleViewInNum(row)">{{
              row.inNum
            }}</el-button>
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[9].visible"
          label="退库数量"
          min-width="100"
          align="center"
          prop="tkNum"
          :show-overflow-tooltip="true"
        >
          <template v-slot="{ row }">
            <el-button link type="primary" @click="handleViewTKNum(row)">{{
              row.tkNum
            }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="领用/出库" align="center">
          <el-table-column
            v-if="columns[10].visible"
            label="数量"
            min-width="100"
            align="center"
            prop="useNum"
            :show-overflow-tooltip="true"
          >
            <template v-slot="{ row }">
              <el-button
                link
                type="primary"
                @click="handleViewUseNum(row, undefined)"
                >{{ row.useNum }}</el-button
              >
            </template>
          </el-table-column>
          <el-table-column
            v-if="columns[11].visible"
            label="一车间"
            align="center"
            min-width="100"
            prop="useNumY"
            :show-overflow-tooltip="true"
          >
            <template v-slot="{ row }">
              <el-button
                link
                type="primary"
                @click="handleViewUseNum(row, '一车间')"
                >{{ row.useNumY }}</el-button
              >
            </template>
          </el-table-column>
          <el-table-column
            v-if="columns[12].visible"
            label="四车间"
            align="center"
            min-width="100"
            prop="useNumS"
            :show-overflow-tooltip="true"
          >
            <template v-slot="{ row }">
              <el-button
                link
                type="primary"
                @click="handleViewUseNum(row, '四车间')"
                >{{ row.useNumS }}</el-button
              >
            </template>
          </el-table-column>
        </el-table-column>
        <el-table-column
          v-if="columns[13].visible"
          label="调入数量"
          min-width="100"
          align="center"
          prop="allotInNumber"
          :show-overflow-tooltip="true"
        >
          <template v-slot="{ row }">
            <el-button link type="primary" @click="handleViewAllot(row)">{{
              row.allotInNumber
            }}</el-button>
          </template>
        </el-table-column>
        <el-table-column
          v-if="columns[14].visible"
          label="调出数量"
          min-width="100"
          align="center"
          prop="allotOutNumber"
          :show-overflow-tooltip="true"
        >
          <template v-slot="{ row }">
            <el-button link type="primary" @click="handleViewAllot(row)">{{
              row.allotOutNumber
            }}</el-button>
          </template>
        </el-table-column>
        <el-table-column
          label="盈亏"
          min-width="120"
          align="center"
          inputmode="numeric"
          prop="ykNumber"
          v-if="columns[1].visible"
        >
        <template #default="scope">
            <el-button
              type="primary"
              @click="handleViewYKNumber(scope.row)"
              text
            >
              {{ scope.row.ykNumber }}
            </el-button>
          </template>
      </el-table-column>
        <el-table-column
          v-if="columns[15].visible"
          label="库存数量"
          min-width="100"
          align="center"
          prop="quantity"
          :show-overflow-tooltip="true"
        >
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script setup name="PaperSummaryQuery">
import { getPaperCirculationList,exportPaperCirculationList } from "@/api/storage/paperDataStatistics.js";
import { downLoad } from "@/utils/ruoyi";
const { proxy } = getCurrentInstance();
const loading = ref(true);
const dataList = ref([]);
const showSearch = ref(true);
const router = useRouter();
const total = ref(0);
const advanced = ref(false);
const flag = ref(true);

// 列显隐信息
const columns = ref([
  { key: 0, label: `平张/卷筒`, visible: true },
  { key: 1, label: `纸张编码`, visible: true },
  { key: 2, label: `纸张名称`, visible: true },
  { key: 3, label: `规格型号`, visible: true },
  { key: 4, label: `定量`, visible: true },
  { key: 5, label: `品牌`, visible: true },
  { key: 6, label: `单位`, visible: true },
  { key: 7, label: `起初数量`, visible: true },
  { key: 8, label: `入库数量`, visible: true },
  { key: 9, label: `领用数量`, visible: true },
  { key: 10, label: `一车间`, visible: true },
  { key: 11, label: `四车间`, visible: true },
  { key: 12, label: `盘点数量`, visible: true },
  { key: 13, label: `调拨入库数量`, visible: true },
  { key: 14, label: `调拨出库数量`, visible: true },
  { key: 15, label: `库存数量`, visible: true },
  { key: 16, label: `备注`, visible: true },
]);

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
});

const getDateNow = () => {
  var myDate = new Date();
  var year = myDate.getFullYear();
  var month = myDate.getMonth() + 1; //获取当前月份(0-11,0代表1月，所以要加1);
  var day = myDate.getDate(); //获取当前日（1-31）
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  var firstDay = year + "-" + month + "-01";
  return firstDay;
};
const getCurrentDay = () => {
  var now = new Date();
  var year = now.getFullYear();
  var month = now.getMonth() + 1; // 月份从0开始，需要加1
  var day = now.getDate();
  if (month < 10) {
    month = "0" + month;
  }
  if (day >= 0 && day <= 9) {
    day = "0" + day;
  }
  const currentDay = year + "-" + month + "-" + day;
  return currentDay;
};
const dateRange = ref([getDateNow(), getCurrentDay()]);
const { queryParams } = toRefs(data);
const {
  mes_paper_type,
  mes_paper_package,
  mes_paper_specs,
  mes_paper_brand,
  mes_paper_unit,
} = proxy.useDict(
  "mes_paper_type",
  "mes_paper_package",
  "mes_paper_specs",
  "mes_paper_brand",
  "mes_paper_unit"
);

const getList = () => {
  loading.value = true;
  queryParams.value.startDate = Array.isArray(dateRange.value)
    ? dateRange.value[0]
    : undefined;
  queryParams.value.endDate =
    Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined;
  getPaperCirculationList(queryParams.value)
    .then((res) => {
      loading.value = false;
      dataList.value = res.rows;
      total.value = res.total;
    })
    .catch((err) => {
      loading.value = false;
    });
};

// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  dateRange.value = [getDateNow(), getCurrentDay()];
  proxy.resetForm("queryRef");
  handleQuery();
};

// 查看
const handleView = (row) => {
  router.push({
    path: "/basicInfo/paperCoding/details",
    query: {
      id: row.id,
      code: row.code,
      readonly: true,
    },
  });
};

//入库明细
const handleViewInNum = (row) => {
  router.push({
    path: "/storage/searchPaper",
    query: {
      id: row.id,
      code: row.code,
      startDate: Array.isArray(dateRange.value)
        ? dateRange.value[0]
        : undefined,
      endDate:
        Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined,
      readonly: true,
    },
  });
};

//盈亏：盘点明细数据穿透
const handleViewYKNumber = (row) => {
  router.push({
    path: "/production/searchMachineInventory/inventorySheetDetail",
    query: {
      paperId: row.id,
      machineId: row.machineId,
      startDate:Array.isArray(dateRange.value)? dateRange.value[0]: undefined,
      endDate:Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined,
      info: true,
    },
  });
};

const handleViewUseNum = (row, workshopName) => {
  router.push({
    path: "/storage/searchAccess",
    query: {
      id: row.id,
      code: row.code,
      workshopName: workshopName,
      startDate: Array.isArray(dateRange.value)
        ? dateRange.value[0]
        : undefined,
      endDate:
        Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined,
      readonly: true,
    },
  });
};

const handleViewTKNum = (row, workshopName) => {
  router.push({
    path: "/storage/storage/searchCancellation/index",
    query: {
      id: row.id,
      code: row.code,
      workshopName: workshopName,
      startDate: Array.isArray(dateRange.value)
        ? dateRange.value[0]
        : undefined,
      endDate:
        Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined,
      readonly: true,
    },
  });
};

const handleViewAllot = (row) => {
  router.push({
    path: "/storage/searchTransfer",
    query: {
      id: row.id,
      code: row.code,
      startDate: Array.isArray(dateRange.value)
        ? dateRange.value[0]
        : undefined,
      endDate:
        Array.isArray(dateRange.value) > 0 ? dateRange.value[1] : undefined,
      readonly: true,
    },
  });
};

// 导出
const handleExport = () => {
  exportPaperCirculationList(queryParams.value).then((res) => {
    downLoad(res.msg, "纸张流转汇总统计");
  });
};

getList();
</script>

<style lang="scss" scoped>
.operation {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-bottom: 18px;
}
</style>